<?php

namespace App\Service\Common;

use App\Enums\OpenShop\BaseEnum;
use Illuminate\Support\Facades\Http;

class HolidayService
{
    /**
     * 获取一整年的节日信息
     * @param $date
     * @return array|mixed
     */
    public static function getHoliday($date)
    {
        $res = Http::withHeaders([
            'Content-Type' => 'application/json; charset=utf-8',
        ])->get(sprintf(config('holiday.url'), $date))->json();
        if (!isset($res['code']) || $res['code'] != 0) {
            return [];
        }
        return $res;
    }


    public static function getHolidayByDay()
    {
        $info = self::getHoliday(date('Y'));
        if (empty($info)) {
            return 0;
        }
        $isHoliday = BaseEnum::HOLIDAY_FALSE;
        $week = date('w') ?: BaseEnum::WEEK_SUNDAY;
        if (in_array($week, BaseEnum::WEEKEND)) {
            $isHoliday = BaseEnum::HOLIDAY_TRUE;
        }
        $holidayArr = $info['holiday'] ?? [];
        foreach ($holidayArr as $k => $value) {
            if ($k == date('m-d')) {
                if ($value['holiday'] == true) {
                    $isHoliday = BaseEnum::HOLIDAY_TRUE;
                } else {
                    $isHoliday = BaseEnum::HOLIDAY_FALSE;
                }
            }
        }
        return $isHoliday;
    }

}
